package org.jeecg.modules.quality.service.impl;

import org.jeecg.modules.quality.entity.BussInspectionStandards;
import org.jeecg.modules.quality.entity.BussInspectionStandardsDetail;
import org.jeecg.modules.quality.mapper.BussInspectionStandardsDetailMapper;
import org.jeecg.modules.quality.mapper.BussInspectionStandardsMapper;
import org.jeecg.modules.quality.service.IBussInspectionStandardsService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 检验标准维护
 * @Author: jeecg-boot
 * @Date:   2024-07-03
 * @Version: V1.0
 */
@Service
public class BussInspectionStandardsServiceImpl extends ServiceImpl<BussInspectionStandardsMapper, BussInspectionStandards> implements IBussInspectionStandardsService {

	@Autowired
	private BussInspectionStandardsMapper bussInspectionStandardsMapper;
	@Autowired
	private BussInspectionStandardsDetailMapper bussInspectionStandardsDetailMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(BussInspectionStandards bussInspectionStandards, List<BussInspectionStandardsDetail> bussInspectionStandardsDetailList) {
		bussInspectionStandardsMapper.insert(bussInspectionStandards);
		if(bussInspectionStandardsDetailList!=null && bussInspectionStandardsDetailList.size()>0) {
			for(BussInspectionStandardsDetail entity:bussInspectionStandardsDetailList) {
				//外键设置
				entity.setStandardsId(bussInspectionStandards.getId());
				bussInspectionStandardsDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(BussInspectionStandards bussInspectionStandards,List<BussInspectionStandardsDetail> bussInspectionStandardsDetailList) {
		bussInspectionStandardsMapper.updateById(bussInspectionStandards);
		
		//1.先删除子表数据
		bussInspectionStandardsDetailMapper.deleteByMainId(bussInspectionStandards.getId());
		
		//2.子表数据重新插入
		if(bussInspectionStandardsDetailList!=null && bussInspectionStandardsDetailList.size()>0) {
			for(BussInspectionStandardsDetail entity:bussInspectionStandardsDetailList) {
				//外键设置
				entity.setStandardsId(bussInspectionStandards.getId());
				bussInspectionStandardsDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		bussInspectionStandardsDetailMapper.deleteByMainId(id);
		bussInspectionStandardsMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			bussInspectionStandardsDetailMapper.deleteByMainId(id.toString());
			bussInspectionStandardsMapper.deleteById(id);
		}
	}
	
}
